clear all 
set more off

use "..\data\contracts_for_ramey_merge.dta", clear 

// Format dates 
gen year = floor(quarter)
gen qtr  = (quarter-year)*4 + 1
gen date = yq(year, qtr)
format date %tq 

tsset date 

gen G = contract_proxy + wages + non_wage 

// Generate one-quarter log change in G and its components
foreach vv in G contract_proxy wages non_wage {
gen log_`vv' = log(`vv')
sort date
gen dl`vv'   = 100*(log_`vv' - L.log_`vv')
gen dl4`vv'  = 100*(log_`vv' - L4.log_`vv') 
}

// Calculate the shapley value for each component 
reg dlG dlcontract_proxy dlwages dlnon_wage if year > 1960, nocons
predict dlG_hat 

reg dlG_hat dlcontract_proxy dlwages dlnon_wage if year > 1960
shapley2, stat(r2) // Results replicate the first column of Table 1 

/* For the variance decomposition, we calculate the contribution to the
total variance of the growth rate of G, \sigma_g^2, as component i's share of g
squared, \alpha_i^2, times the variance of component i, \sigma_i^2. That is: 
i's contribution to total variance is: (\alpha_i^2*\sigma_i^2)/\sigma_g^2 */ 

// Component weights are taken as the coefficients from this regression: 
reg dlG_hat dlcontract_proxy dlwages dlnon_wage if year > 1960

local share_contracts _b[dlcontract_proxy] 
local share_wages     _b[dlwages] 
local share_residual  _b[dlnon_wage] 

// Variances are taken from the following: 
sum  dlG dlcontract_proxy dlwages dlnon_wage if year > 1960 





